﻿var can;
var ctx;

var can2;
var ctx2;

var can3;
var ctx3;

var wave = new Array();
var curWave = 0;
var maxWaves = 30;

var bubble = new Array();
var curBubble = 0;
var maxBubble = 20;

var CB = new Array();
var curCB = 0;
var maxCB = 5;

var fishes;

var mousex;
var mousey;

var memo;
var memoC = 0;

var jelly = new Array();
var maxJelly = 5;

var ane;

var group = new Array(); //鱼群
var world = {
	x: 400,
	y: 300,
	r: 600
};

var backG;

var anemoneIMG = new Image();
var jellyIMG = new Image();

var time = 0;
var lasttime;

var dark;

var dtime = 0;
var dlasttime;


function init() {

	can = document.getElementById('canvas');
	ctx = can.getContext('2d');

	can2 = document.getElementById('bg');
	ctx2 = can2.getContext('2d');


	can.addEventListener('mousedown', onMouseDown, false);
	can.addEventListener('touchstart', onMouseDown, false);

	anemoneIMG.src = "anemone.png";
	jellyIMG.src = "jelly.png";

	for (var i = 0; i < maxWaves; i++) {
		var t = new circle();
		wave.push(t);
	} //创建maxWaves个涟漪
	for (i = 0; i < maxBubble; i++) {
		var g = new bubbleObj();
		bubble.push(g);
	} //创建maxBubble个泡泡
	for (i = 0; i < maxCB; i++) {
		var f = new cbObj();
		CB.push(f);
	} //创建maxCB个泡泡

	memo = new memoObj();
	for(i = 0; i < memo.num; i++){
		memo.init(i);
	}

	ane = new anemoneObj();
	ane.init();

	for (i = 0; i < maxJelly; i++) {
		var e = new jellyObj();
		jelly.push(e);
		jelly[i].init();
	} //创建maxJelly个jelly
	fishes = new Flocking();
	fishes.init(800, 600, 2);

	lasttime = Date.now();

	backG = new bgArg();
	backG.begin(); //初始化背景参数
	mousex = fishes.x || 0;
	mousey = fishes.y || 0;

	dark = new darkObj();

	blasttime = Date.now();

}
function onMouseDown(e) {
	mousex = e.offsetX;
	mousey = e.offsetY;
	if (event.offsetX) // on mouse down
	{

		fishes.target(e.offsetX, e.offsetY);

	}

	if (e.pageX) {


		wave[curWave].begin(mousex, mousey); //鼠标点击时在鱼处画涟漪
		curWave++;
		if (curWave == maxWaves) curWave = 0;



		for (var j = 0; j < 2; j++) {
			if (fishes.boids[j].spd >= fishes.boids[j].minspd) {
				for (var i = 0; i < 2; i++) {
					CB[curCB].begin(fishes.boids[i].x, fishes.boids[i].y); //鼠标点击时在鱼处画涟漪
					curCB++;
					if (curCB == maxCB) curCB = 0;
				}

				fishes.boids[j].spd -= 0.05;
			}

		}



	}
}

function gameloop() {
	window.requestAnimFrame(gameloop);


	ctx.clearRect(0, 0, can.width, can.height);

	drawWaves();
	drawCB();
	drawBubble();
	drawmemos();

	dark.draw();
	fishes.updata(time);
	ane.draw();

	//drawJelly();
	
	memoDetect();
	jellyDetect();
	
	bubbleUp();
	aneSpeed();

	colorBack();//随时间颜色回退

	time = Date.now() - lasttime;
	lasttime = Date.now();

}

function drawPartners() {
	for (var i = 0; i < maxPartners; i++) {
		if (partners[i].recruit == 0) {
			partners[i].grpFalse();
		} else if (partners[i].recruit == 1) {
			partners[i].grpTrue();
		}
	} //画partners
}

function drawWaves() {
	var m = curWave;
	for (var i = 0; i < maxWaves; i++, m++) {
		if (m == maxWaves) m = 0;
		if (wave[m].life > 0) {
			wave[m].draw();
		}
	} //如果涟漪有生命值，就画出来
}

function drawBubble() {
	var n = curBubble;
	for (var i = 0; i < maxBubble; i++, n++) {
		if (n == maxBubble) n = 0;
		if (bubble[n].alpha > 0 && bubble[n].alpha < 1) {
			bubble[n].draw();
		}
	}
}

function drawmemos() {
		memo.draw();
}

function game() {
	init();
	gameloop();
	drawBG();
}